package cn.dglydrpy.fundmanagement.service.impl;

import cn.dglydrpy.fundmanagement.dao.entiey.TaxCalculation;
import cn.dglydrpy.fundmanagement.dao.mapper.TaxCalculationMapper;
import cn.dglydrpy.fundmanagement.service.ITaxCalculationService;
import cn.dglydrpy.fundmanagement.service.dto.TaxCalculationDto;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TaxCalculationService implements ITaxCalculationService {

    @Autowired
    private TaxCalculationMapper taxCalculationMapper;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public int saveTaxCalculation(TaxCalculationDto taxCalculationDto) {
        TaxCalculation taxCalculation = new TaxCalculation();
        BeanUtils.copyProperties(taxCalculationDto, taxCalculation);
        taxCalculation.setCreatedTime(sdf.format(new Date()));
        taxCalculation.setUpdatedTime(sdf.format(new Date()));
        if (taxCalculation.getStatus() == null || taxCalculation.getStatus().isEmpty()) {
            taxCalculation.setStatus("1"); // 1-有效 0-无效
        }
        return taxCalculationMapper.insert(taxCalculation);
    }

    @Override
    public void updateTaxCalculation(TaxCalculationDto taxCalculationDto) {
        TaxCalculation taxCalculation = new TaxCalculation();
        BeanUtils.copyProperties(taxCalculationDto, taxCalculation);
        taxCalculation.setUpdatedTime(sdf.format(new Date()));
        taxCalculationMapper.updateById(taxCalculation);
    }

    @Override
    public void deleteTaxCalculation(Long id) {
        taxCalculationMapper.deleteById(id);
    }

    @Override
    public TaxCalculationDto getTaxCalculationById(Long id) {
        TaxCalculation taxCalculation = taxCalculationMapper.selectById(id);
        if (taxCalculation == null) {
            return null;
        }
        TaxCalculationDto taxCalculationDto = new TaxCalculationDto();
        BeanUtils.copyProperties(taxCalculation, taxCalculationDto);
        return taxCalculationDto;
    }

    @Override
    public Page<TaxCalculationDto> getTaxCalculationsByConditions(String fundCode, String portfolioId, int pageNo, int pageSize) {
        Page<TaxCalculation> page = new Page<>(pageNo, pageSize);
        QueryWrapper<TaxCalculation> queryWrapper = new QueryWrapper<>();
        if (fundCode != null && !fundCode.isEmpty()) {
            queryWrapper.like("fund_code", fundCode);
        }
        if (portfolioId != null && !portfolioId.isEmpty()) {
            queryWrapper.eq("portfolio_id", portfolioId);
        }
        queryWrapper.eq("status", "1");
        queryWrapper.orderByDesc("created_time");
        taxCalculationMapper.selectPage(page, queryWrapper);

        Page<TaxCalculationDto> dtoPage = new Page<>(pageNo, pageSize, page.getTotal());
        List<TaxCalculationDto> records = page.getRecords().stream().map(taxCalculation -> {
            TaxCalculationDto dto = new TaxCalculationDto();
            BeanUtils.copyProperties(taxCalculation, dto);
            return dto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(records);
        return dtoPage;
    }

    @Override
    public List<TaxCalculationDto> getAllTaxCalculations() {
        QueryWrapper<TaxCalculation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "1");
        queryWrapper.orderByDesc("created_time");
        List<TaxCalculation> taxCalculations = taxCalculationMapper.selectList(queryWrapper);
        return taxCalculations.stream().map(taxCalculation -> {
            TaxCalculationDto dto = new TaxCalculationDto();
            BeanUtils.copyProperties(taxCalculation, dto);
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public void calculateTaxForTransaction(Long transactionId) {
        // 这里应该实现基于单笔交易计算税额的逻辑
        // 简化实现，实际应该查询交易记录并根据持有期计算适用税率和应缴税额
    }

    @Override
    public void calculateTaxForFund(String fundCode) {
        // 这里应该实现基于基金所有交易记录计算总税额的逻辑
        // 简化实现，实际应该查询基金的所有交易记录并分别计算税额
    }

    @Override
    public void calculateTaxForPortfolio(Long portfolioId) {
        // 这里应该实现基于投资组合中所有基金交易记录计算总税额的逻辑
        // 简化实现，实际应该查询组合中所有基金的交易记录并分别计算税额
    }
}